package com.example.musicplayerv0;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.FileDescriptor;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    public static final int PROGRESS = 101;
    Button mBtnPlay; // 1 与界面btn_play绑定
    Button mBtnPause;
    Button mBtnStop; // 停止播放
    MediaPlayer mediaPlayer;
    TextView mTvStatus;
    SeekBar mSeekbar;
    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if(msg.what == PROGRESS){
                int progress = msg.arg1;
                mSeekbar.setProgress(progress);// 更新进度
            }
            return true; //消息已经处理完成
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();       initEvent();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try { Thread.sleep(1000);
                        if(mediaPlayer!=null && mSeekbar!=null) {
                            int currentPosition = mediaPlayer.getCurrentPosition();  // 23000
                            int duration = mediaPlayer.getDuration();             // 50000
                            int progress = (int) ((currentPosition * 1.0 / duration) * 100); //0~100
                            // 获取 消息空对象
                            Message message = mHandler.obtainMessage();
                            message.what = PROGRESS;  // 消息的类型
                            message.arg1 = progress;
                            mHandler.sendMessage(message);
                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
    }
    private void initEvent() {
        mBtnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mediaPlayer!=null){
                    mediaPlayer.stop();
                    mTvStatus.setText("停止");
                }
            }
        });
        // 3 设置事件监听者
        mBtnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mediaPlayer!=null) {
                    mediaPlayer.pause();// 暂停
                    mTvStatus.setText("暂停");
                }
            }
        });
        mBtnPlay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(mediaPlayer!=null && !mediaPlayer.isPlaying()){
                    mediaPlayer.start();
                    return;
                }
                // 4 处理事件，播放音乐
                mediaPlayer = new MediaPlayer();
                try {
                    AssetFileDescriptor afd = getResources().getAssets().openFd("yinyue1.mp3");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        mediaPlayer.setDataSource(afd);
                    }else{
                        FileDescriptor fd = afd.getFileDescriptor();
                        mediaPlayer.setDataSource(fd,afd.getStartOffset(),afd.getLength());
                    }
                    //mediaPlayer.prepare();
                    //mediaPlayer.start();
                    mediaPlayer.prepareAsync();// 异步准备
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mediaPlayer.start();
                            mTvStatus.setText("播放");
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        });
    }
    private void initView() {
        // 2 找到控件
        mBtnPlay = findViewById(R.id.btn_play);
        mBtnPause = findViewById(R.id.btn_pause);
        mBtnStop = findViewById(R.id.btn_stop);
        mTvStatus = findViewById(R.id.tv_status);
        mSeekbar = findViewById(R.id.sb_progress);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mediaPlayer!=null) {
            mediaPlayer.stop();
            mediaPlayer = null;
        }
    }
}
